//
//  WMEClubNewsViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/27.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEClubNewsViewController.h"
#import <Masonry.h>
#import <AFNetworking.h>
#import "WMECludModel.h"
#import "WMETextTableViewCell.h"
#import "WMEPhotoTableViewCell.h"
//#import "WMEClubInformationModel.h"
#import "WMEClubInformationViewController.h"
#import "WMEClubHeadView.h"
#import "WMEClubNewsHeadView.h"
#import <MJRefresh.h>
#import "WMEImageViewController.h"

#pragma mark - club的详情的点击跳转到的VC 查看具体评论 从 clubInformationViewController push 而来
@interface WMEClubNewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;/**<主视图*/
@property (nonatomic, retain)NSMutableArray *dataArr;/**<数据数组*/
@property (nonatomic, copy)NSString *urlStr;/**<网络请求*/
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)NSNumber *next_start;/**<下次开始位置*/
@end

@implementation WMEClubNewsViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_urlStr release];
    [_dic release];
    [_next_start release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //标题栏
    self.navigationItem.title = _clubDic[@"name"];
    [self createTableView];
    [self data];
    [self loadingFunction];
}
#pragma mark - 创建视图
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    
    _tableView.dataSource = self;
    
    //创建头部视图
    WMEClubNewsHeadView *head = [[WMEClubNewsHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    head.NewsDic = _clubDic;
    //添加头部视图
    _tableView.tableHeaderView = head;
    [self.view addSubview:_tableView];
    __block __typeof(&*self) bs = self;
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bs.view.mas_right);
        make.left.equalTo(bs.view.mas_left);
        make.top.equalTo(bs.view.mas_top);
        make.bottom.equalTo(bs.view.mas_bottom);
    }];
    //隐藏 分隔线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView release];
    [head release];
}

#pragma mark - 数据请求
- (void)data
{
    NSString *tempStr = @"http://203.80.144.212/napi/topic/list/by_club/?platform_version=4.4.4&device_platform=L36h&screen_width=1080&screen_height=1776&__domain=www.duitang.com&app_version=67&start=0&club_id=54aa79f8a3101a0f75731ca7&platform_name=Android&locale=zh&app_code=nayutas";
    self.urlStr = [tempStr stringByReplacingOccurrencesOfString:@"club_id=54aa79f8a3101a0f75731ca7" withString:[NSString stringWithFormat:@"club_id=%@", _clubDic[@"id"]]];
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:_urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        NSArray *arr = responseObject[@"data"][@"object_list"];
        self.next_start = responseObject[@"data"][@"next_start"];

        self.dataArr = [WMECludModel modelHandlerWithArray:arr];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}
#pragma mark - tableView的协议方法
#pragma mark - cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

#pragma mark -cell设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WMECludModel *m = _dataArr[indexPath.row];
    if (m.path.length != 0) {
        //有图片的cell
        [tableView registerClass:[WMEPhotoTableViewCell class] forCellReuseIdentifier:@"cell1"];
        WMEPhotoTableViewCell *photoCell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        photoCell.model = m;
        return photoCell;
    }else{
        //没有图片的cell
        [tableView registerClass:[WMETextTableViewCell class] forCellReuseIdentifier:@"cell"];
        WMETextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.model = m;
        return cell;
    }
}

#pragma mark -cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WMECludModel *model = _dataArr[indexPath.row];
    CGFloat h = [WMETextTableViewCell heightWithText:model.content];
    //    NSLog(@"%lf", h);
    if (h > (15.51 * 2)) {
        h = 15.51 * 2 + 1;
    }
    if (model.path) {
        return 80 + 75.5 + h;
    }
    return 60.5 + h;
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSLog(@"%ld", (long)indexPath.row);
    WMECludModel *model = _dataArr[indexPath.row];
    
    NSString *temp = @"http://203.80.144.213/napi/topic/detail/?include_fields=share_links&platform_version=4.4.4&device_platform=L36h&__dtac=%257B%2522_r%2522%253A%2520%2522187455%2522%257D&screen_width=1080&screen_height=1776&__domain=www.duitang.com&app_version=67&platform_name=Android&locale=zh&app_code=nayutas&topic_id=126492";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"id=126492" withString:[NSString stringWithFormat:@"id=%@", model.selfId.stringValue]];
    //AFN网络请求
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.dic = responseObject[@"data"];
        [_tableView reloadData];
        WMEClubInformationViewController *informationView = [[WMEClubInformationViewController alloc] init];
        informationView.interface = model.selfId.stringValue;
        informationView.headDic = _dic;
        informationView.commentCount = model.comment_count;
        [self.navigationController pushViewController:informationView animated:YES];
        [informationView release];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}

#pragma mark -加载

- (void)loadingFunction
{
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)loadMoreData
{
    NSString *newUrlStr = [_urlStr stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%@", _next_start]];
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:newUrlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", responseObject);
        NSArray *arr = responseObject[@"data"][@"object_list"];
        self.next_start = responseObject[@"data"][@"next_start"];
        NSArray *tempArr = [WMECludModel modelHandlerWithArray:arr];
        [self.dataArr addObjectsFromArray:tempArr];
        [_tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        //        if (_next_start.integerValue > self.commentCount.integerValue) {
        //            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        //        }else{
        //            [self.tableView.mj_footer endRefreshing];
        //        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
    
}
//- (void)pathTap:(NSString *)str Width:(NSNumber *)width Height:(NSNumber *)height
//{
//    WMEImageViewController *imgView = [[WMEImageViewController alloc] init];
//    imgView.imgStr = str;
//    imgView.width = width;
//    imgView.height = height;
//    [self.navigationController pushViewController:imgView animated:YES];
//    []
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
